
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html lang="en">
<head>
    <title>Update database</title>
</head>
<body>
<!-- Progress bar holder -->
<div id="progress" style="width:500px;border:1px solid #ccc;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
<?php
require_once("DatabaseConnect.php");

$con=Remoteconnect();
if($con==null){
	$con=Localconnect();	
}
$completed=0;


//stoping the page from timeing out
set_time_limit(0);
$Symbols=array();
//Array of all the symbols from databse
$result = mysqli_query($con,"SELECT * FROM  `company info`");



while($row = mysqli_fetch_array($result)) {
	array_push($Symbols, $row['Symbol']);

  
}



ob_end_flush();
// Total processes

$total = sizeof($Symbols);
$count=0;

foreach ($Symbols as $Symbol){
	$count+=1;
	$stopfirstDataline=0;
	
	// Calculate the percentation
	$percent = intval($count/$total * 100)."%";
	// Javascript for updating the progress bar and information
    echo '<script language="javascript">
    document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#ddd;\">&nbsp;</div>";
    document.getElementById("information").innerHTML="Processing Symbol '.$count.'->'.$Symbol.' of '.$total.' symbols.";
    </script>';
	
	// This is for the buffer achieve the minimum size in order to flush data
    echo str_repeat(' ',1024*64);
// Send output to browser immediately






//initiating  cURL
$curl=curl_init();
//CSV URL obtains all the historical data from yahoo.
curl_setopt ($curl,CURLOPT_URL,"http://ichart.finance.yahoo.com/table.csv?s=".$Symbol."&a=11&b=4&c=1700&d=11&e=6&f=2099");


curl_setopt ($curl,CURLOPT_HEADER,0);
ob_start();
curl_exec ($curl);
curl_close ($curl);
$data=ob_get_clean(); 
//Spltting the data into each days market data
$data=explode("\n",$data);


foreach ($data as $datalineresults){

	
	
	$stopfirstDataline++;
	
	$Date;
	$Open;
	$High;
	$Low;
	$Close;
	$Volume;
	
	if($stopfirstDataline>1){
	//splitting each days market data into variables
	$results=explode(",",$datalineresults);
	

    //Checks to make sure that the data to be added to the database is not NULL
	if(isset($results[0])&isset($results[1])&isset($results[2])&isset($results[3])&isset($results[4])&isset($results[5])){  

	$Date=$results[0];
	$Open=$results[1];
	$High=$results[2];
	$Low=$results[3];
	$Close=$results[4];
	$Volume=$results[5];
	
	echo "Symbol: $Symbol<br>";
	echo "Date: $Date<br>";
	echo "Open: $Open<br>";
	echo"High: $High<br>";
	echo"Low: $Low<br>";
	echo"Close: $Close<br>";
	echo"Volume: $Volume<br>";
	
	//Connection to the database
	

		
	//Execution of the variable	
	$sql="INSERT INTO `shares` (`Date`, `Symbol`, `Open`, `High`, `Low`, `Close`, `Volume`) VALUES ('$Date', '$Symbol', '$Open', '$High', '$Low', '$Close', '$Volume')";
	
	//error checking of sql queries
if (!mysqli_query($con,$sql))
{
  echo('Error: ' . mysqli_error($con)."<br>");
}

	//mysqli_query($con,$sql);


	

	
	

	}

	
	
	
	
	
  	
	}

}
	
    flush();




}


echo '<script language="javascript">document.getElementById("information").innerHTML="Process completed"</script>';
?>
</body>
</html>